package view;

import dao.BaseDao;
import entity.ChessUser;
import message.LoginMessage;
import socket.ChessClient;
import socket.ChessServer;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录框,如果没登陆则注册.
 *
 * @author 任禹名 张天羿
 */

public class LoginDialog extends JDialog {
    private GameFrame gameFrame;
    private LoginDialog loginDialog = this;
    private JTextField nameTextField = new JTextField();
    private JLabel nameTip = new JLabel("请输入姓名");
    private JButton ok = new JButton("确定");
    private JButton cancel = new JButton("取消");


    public LoginDialog(GameFrame gameFrame) {
        BaseDao.getConnection();
        //主界面设置
        ChessClient.getInstance().setLoginDialog(this);
        this.setModal(true);
        this.gameFrame = gameFrame;
        this.setResizable(false);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setBounds((int) (gameFrame.getWidth() * 0.4), (int) (gameFrame.getHeight() * 0.4), 260, 150);
        this.setLayout(null);
        //配件设置
        nameTip.setBounds(30, 20, 100, 15);
        nameTextField.setBounds(30, 40, 200, 30);
        ok.setBounds(30, 80, 90, 30);
        cancel.setBounds(140, 80, 90, 30);
        //添加配件
        this.add(nameTip);
        this.add(ok);
        this.add(nameTextField);
        this.add(cancel);

        addAction();
        this.setVisible(true);
    }

    /**
     * 挂载点击事件.
     */
    private void addAction() {
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!ChessClient.getInstance().isConnected()) {
                    if (!ChessClient.getInstance().connect()) {
                        return;
                    }
                }
                System.out.println("网络成功连接");
                String name = nameTextField.getText();
                if (name.trim() != "") {
                    ChessUser chessUser = ChessServer.getInstance().getChessUserService().findUser(name);
                    if (chessUser == null) {
                        String[] options = {"创建", "重新输入"};
                        int res = JOptionPane.showOptionDialog(null, "确定创建新用户吗", "当前输入的名字是新用户",
                                JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION,
                                null, options, options[0]);
                        if (res == 0) {
                            loginDialog.dispose();
                            chessUser = new ChessUser(name);
                            new UpdatePicture(chessUser);
                        }
                    } else {
                        loginDialog.dispose();
                        LoginMessage loginMessage = new LoginMessage(chessUser.getName());
                        ChessClient.getInstance().sendMessage(loginMessage);
                    }
                }
            }
        });
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginDialog.dispose();
            }
        });
    }

    /**
     * 登录成功跳转房间列表.
     *
     * @param chessUser
     * @see RoomList
     */
    public void loginSuc(ChessUser chessUser) {
        toRoomList(chessUser);
        loginDialog.dispose();
    }

    public void toRoomList(ChessUser chessUser) {
        new RoomList(this.gameFrame, chessUser);
        this.gameFrame.setVisible(false);
        loginDialog.dispose();
    }

    /**
     * 已登录提示
     */
    public void showMessage() {
        JOptionPane.showMessageDialog(null, "该帐号已登录", "", JOptionPane.WARNING_MESSAGE);
    }

    /**
     * 登录失败提示
     */
    public void loginFail() {
        JOptionPane.showMessageDialog(null, "登录失败", "", JOptionPane.WARNING_MESSAGE);
    }
}
